<?php
class DomFgTreeNode {
	
	private $name;
	
	private $icon;
	
	private $link;
	
	private $isOpen = true;
	
	private $selected = false;
	
	private $children = array();
	
	public function __construct( $name ) {
		$this->name($name);
	}
	
	public static function c( $value ) {
		return new DomFgTreeNode( $value );
	}
	
	public function name( $value ) {
		$this->name = $value;
	}
	
	public function link( $value ) {
		$this->link = $value;
	}
	
	public function icon( $value ) {
		$this->icon = $value;
	}
	
	public function selected( $value ) {
		$this->selected = (bool)$value;
	}
	
	public function isOpen( $value ) {
		$this->isOpen = (bool)$value;
	}
	
	public function isLeaf() {
		return empty($this->children);
	}
	
	public function add( DomFgTreeNode $node ) {
		$this->children[] = $node;
		return $this;
	}
	
	public function set( array $nodes ) {
		foreach( $nodes as $node ) {
			$this->add( $node );
		}
		return $this;
	}
	
	public function __get($var ) {
		return $this->$var;
	}
	
	public function __toString() {
		$html = '<li>';
		if( $this->icon )
			$html .= '<span class="'.$this->icon.'">';

		if( $this->selected ) 
			$html .= '<b>';
			
		$html .= $this->link ? '<a href="'.$this->link.'">'.$this->name.'</a>' : $this->name;
		
		if( $this->selected ) 
			$html .= '</b>';
		
		if($this->icon)
			$html .= '</span>';
		
		if( $this->children ) {
			$html .= '<ul '.($this->isOpen ? '' : 'style="display:none;"').'>';
			foreach( $this->children as $child ) {
				$html .= (string)$child;
			}
			$html .= '</ul>';
		}
		return $html.'</li>';
	}


}
?>